package scuba.DiveBuddy.data;

import java.util.Date;

import org.joda.time.DateTime;
import scuba.DiveBuddy.Class.DiverProfile;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * @author Alex Anderson
 * @version 3
 * @since 19/01/14.
 *
 * Containing static methods for accessing and manipulating the UserProfileTable
 */
public class DiverProfileSource {
    private static String TABLE_NAME = "DiverProfile";

    private static String COLUMN_ID = "DiverID";
    private static String COLUMN_FORENAME = "DiverForename";
    private static String COLUMN_SURNAME = "DiverSurname";
    private static String COLUMN_DOB = "UserDOB";
    private static String COLUMN_ICECONTACT = "ICEContact";
    private static String COLUMN_ICENUMBER = "ICENumber";
    private static String COLUMN_ALLERGYNOTES = "AllergyNotes";
    private static String COLUMN_MEDNOTES = "MedicalNotes";

    //TABLE MOD [#STATICS]
    public static void CreateTable(SQLiteDatabase db) {
        String create = "create table " + TABLE_NAME  +" (" +
                COLUMN_ID + " integer primary key autoincrement," +
                COLUMN_FORENAME + " varchar(128) not null," +
                COLUMN_SURNAME + " varchar(128) not null," +
                COLUMN_DOB + " integer not null," +
                COLUMN_ICECONTACT + " varchar(128)," +
                COLUMN_ICENUMBER + " varchar(128)," +
                COLUMN_ALLERGYNOTES + " varchar(256)," +
                COLUMN_MEDNOTES + " varchar(256));";

        Log.w("DATABASE", "Creating table " + TABLE_NAME + " with STATEMENT = '" + create + "'");

        try {
            Log.w("DATABASE", "Created table " + TABLE_NAME);
            db.execSQL(create);
        }
        catch(SQLException sql) {
            Log.e("DATABASE", "Create Failed on " + TABLE_NAME + " with error = '" + sql + "'");
        }
    }

    public static void DropTable(SQLiteDatabase db) {
        String drop = "drop table if exists " + DiverProfileSource.TABLE_NAME;

        Log.w("DATABASE",
                "Dropping table " + DiverProfileSource.TABLE_NAME + " with STATEMENT = '" + drop + "'");

        try {
            Log.w("DATABASE", "Table " + DiverProfileSource.TABLE_NAME + " Dropped");
            db.execSQL(drop);
        }
        catch(SQLException sql) {
            Log.e("DATABASE", "Failed to drop table " + DiverProfileSource.TABLE_NAME + " with error = '" + sql + "'");
        }
    }

    public static void UpgradeTable(SQLiteDatabase db) {
        DropTable(db);
        CreateTable(db);
    }

    //ENTRY MODS
    public static int CreateUser(String forename, String surname, DateTime dob) {

        SQLiteDatabase db = Database.getInstance().getDatabase("DiverProfileSource::CreateUser()");

        ContentValues values = new ContentValues();
        values.put(COLUMN_FORENAME, forename);
        values.put(COLUMN_SURNAME, surname);
        values.put(COLUMN_DOB, dob.getMillis());

        long id = db.insert(TABLE_NAME, null, values);
        return (int)id;
    }

    public static DiverProfile GetActiveProfile()
    {
        //Returns the FIRST User In the Database
        SQLiteDatabase db = Database.getInstance().getDatabase("DiverProfileSource::GetActiveProfile()");
        String query = "SELECT * FROM " + TABLE_NAME;

        Cursor c = db.rawQuery(query, null);
        if(c.getCount() == 0) {
            c.close();
            Log.w("DATABASE", "Query: '" + query + "' returned null");
            return null; //nothing
        }
        c.moveToFirst();

        long date = c.getLong(c.getColumnIndex(COLUMN_DOB));

        //public DiverProfile(int id, String forename, String surname, Date dob, String icecontact, String icenumber, String allergy, String med)
        DiverProfile p = new DiverProfile(
                c.getInt(c.getColumnIndex(COLUMN_ID)),
                c.getString(c.getColumnIndex(COLUMN_FORENAME)),
                c.getString(c.getColumnIndex(COLUMN_SURNAME)),
                new DateTime(date),
                c.getString(c.getColumnIndex(COLUMN_ICECONTACT)),
                c.getString(c.getColumnIndex(COLUMN_ICENUMBER)),
                c.getString(c.getColumnIndex(COLUMN_ALLERGYNOTES)),
                c.getString(c.getColumnIndex(COLUMN_MEDNOTES)));

        c.close();

        Log.w("DATABASE", "returning p=" + p);
        return p;
    }

    //,
    public static Boolean ModifyForename(DiverProfile prof, String forename) {
        String q = "UPDATE " + TABLE_NAME + " SET " + COLUMN_FORENAME + " = '" + forename + "' WHERE " + COLUMN_ID + " = " + prof.getID();

        return Database.getInstance().Exec("DiverProfileSource:ModifyForename", q);
    }

    public static Boolean ModifySurname(DiverProfile prof, String surname) {
        String q = "UPDATE " + TABLE_NAME + " SET " + COLUMN_SURNAME + " = '" + surname + "' WHERE " + COLUMN_ID + " = " + prof.getID();

        return Database.getInstance().Exec("DiverProfileSource:ModifySurname", q);
    }

    public static Boolean ModifyDOB(DiverProfile prof, DateTime dob) {
        String q = "UPDATE " + TABLE_NAME + " SET " + COLUMN_DOB + " = " + dob.getMillis() + " WHERE " + COLUMN_ID + " = " + prof.getID();

        return Database.getInstance().Exec("DiverProfileSource:ModifyDOB", q);
    }

    public static Boolean ModifyICEContact(DiverProfile prof, String contact) {
        String q = "UPDATE " + TABLE_NAME + " SET " + COLUMN_ICECONTACT + " = '" + contact + "' WHERE " + COLUMN_ID + " = " + prof.getID();

        return Database.getInstance().Exec("DiverProfileSource:ModifyICEContact", q);
    }

    public static Boolean ModifyICENumber(DiverProfile prof, String number) {
        String q = "UPDATE " + TABLE_NAME + " SET " + COLUMN_ICENUMBER + " = " + number + " WHERE " + COLUMN_ID + " = " + prof.getID();
        return Database.getInstance().Exec("DiverProfileSource:ModifyICENumber", q);
    }

    public static Boolean ModifyAllergy(DiverProfile prof, String notes) {
        String q = "UPDATE " + TABLE_NAME + " SET " + COLUMN_ALLERGYNOTES + " = " + notes + " WHERE " + COLUMN_ID + " = " + prof.getID();

        return Database.getInstance().Exec("DiverProfileSource:ModifyAllergy", q);
    }

    public static Boolean ModifyMedicine(DiverProfile prof, String notes) {
        String q = "UPDATE " + TABLE_NAME + " SET " + COLUMN_MEDNOTES + " = " + notes + " WHERE " + COLUMN_ID + " = " + prof.getID();

        return Database.getInstance().Exec("DiverProfileSource:ModifyMedicine", q);
    }
}
